var util=require('../../../utils/util.js')
Page({

  data: {
    redtype: [
      { name: '打折', value: '0', checked: 'true'},
      { name: '满减', value: '1'  },     
    ],
    type:0,
    
    reduces:[{full:'',reduce:''}],

    activitystate: [
      { name: '上线', value: '0', checked: 'true' },
      { name: '下线', value: '1' },
    ],
    state:0,
    sendtype: ['每人一次','每人每天一次','不限次'],
    sendtypes: [{ name: '每人一次', value: '0', checked: 'true' },
      { name: '每人每天一次', value: '1' },
      { name: '不限次', value: '2' },],
    sendtypeindex:0, 

    time:{start:{date:'2019-10-11',time:'00:00'},end:{date:'2019-11-10',time:'23:59'}},
    limittime: { start: { start: '2015-10-11', end: '2019-10-11' }, end: { start: '2016-11-10', end: '2019-10-11' }},

    activity:{},

    id:null 
  },
  onLoad:function(options){
    var id = options.id;
    if (id && id != 0){
      this.getredacbbyid(id); 
      this.setData({id:id})
    }
    this.setlimittime();
  },
  setlimittime:function(){
    var datetime=util.getNextTime();
    var limittime = this.data.limittime;
    var dt=datetime.split(" ");
    limittime.start.start = dt[0];
    limittime.end.start = dt[0];
    datetime = util.getNextTime(60*24*365*10);
    dt = datetime.split(" ");
    limittime.start.end = dt[0];
    limittime.end.end = dt[0];
    var time = this.data.time; 
    time.start.date = limittime.start.start;
    time.end.date = limittime.end.start;
    this.setData({
      limittime: limittime,
      time: time
    })
  },
  bindSendType:function(e){
    this.setData({
      sendtypeindex:e.detail.value
    })
    var activity = this.data.activity;
    activity.sendtype = e.detail.value;
    this.setData({
      activity: activity
    })
  },
  activityStateChange:function(e){
    var activity = this.data.activity;
    activity.state = e.detail.value;
    this.setData({
      activity: activity
    })
  },
  bindDiscount:function(e){
    var activity = this.data.activity;
    activity.discount = e.detail.value;
    this.setData({
      activity: activity
    })
  },
  redTypeChange:function(e){
    this.setData({
      type:e.detail.value
    })
    var activity = this.data.activity;
    activity.redtype = e.detail.value;
    this.setData({
      activity: activity
    })
  },
  bindInputName:function(e){
    var activity=this.data.activity;
    activity.title=e.detail.value;
    this.setData({
      activity: activity
    })
  },
  bindInputSummary:function(e){
    var activity = this.data.activity;
    activity.summary = e.detail.value;
    this.setData({
      activity: activity
    })
  },
  bindDateTimeChange: function(e){
    var v=e.detail.value;
    var datatime=e.target.dataset.datetime;
    var startend=e.target.dataset.startend;
    var time = this.data.time;
    time[startend][datatime]=v;
    this.setData({
      time:time
    })
  },
  
  addReduce:function(){
    var reduces = this.data.reduces;
    reduces.push({full:'',reduce:''})
    this.setData({
      reduces: reduces
    })
  },
  bindInputReduce:function(e){
    var index=e.target.dataset.id;
    var v=e.detail.value;
    var reduces=this.data.reduces;
    reduces[index].reduce=v;
    this.setData({
      reduces: reduces
    })
  },
  bindInputFull: function (e) {
    var index = e.target.dataset.id;
    var v = e.detail.value;
    var reduces = this.data.reduces;
    reduces[index].full = v;
    this.setData({
      reduces: reduces
    })
  },
  bindsubmit:function(){
    var activity=this.data.activity;
    var id=this.data.id;
    if (id&&id!=0){
      activity.id=id;
    }

    activity.sendtype=this.data.sendtypes[this.data.sendtypeindex].value;
    activity.redtype=this.data.redtype[this.data.type].value;
    activity.state=this.data.activitystate[this.data.state].value;
    //日期  满减
    var time=this.data.time;
    activity.startTime=time.start.date+" "+time.start.time;  
    activity.endTime = time.end.date + " " + time.end.time;
    if (util.getNextTime())
    if (activity.startTime > activity.endTime) {
      util.showinfo("结束时间要大于开始时间");
      return ;
    }
    if (activity.startTime < util.getNextTime()){
      if (activity.id){
        util.showinfo("已经开始的活动禁止修改");
      }else{
        util.showinfo("活动时间不能小于当前时间");
      }      
      return;  
    }
    activity.reduction=this.data.reduces;
    var url = util.url.shop_redpacket_addredact;//"/red/addredact";
    var data=activity;
    util.fetchPost(
      url,
      data,
      function(ret){
        if(ret.data.id){
          util.showinfo("添加成功");
          wx.redirectTo({
            url: 'redactivity',
          });
        }
      }
    )
  },
  getredacbbyid:function(id){
    var that=this;
    var url = util.url.shop_redpacket_actid +"?id="+id;//"/red/getredactbyid?id="+id;
    util.fetchGet(
      url,
      function(ret){
        var act=ret.data;
        
        that.setredtype(act.redtype);
        that.setredstate(act.state);
        that.setdatetime(act.startTime,act.endTime);
        that.setsendtype(act.sendtype);
        if (act.redtype==1){
          that.setData({ reduces: act.reduction})
        }else{
          act.discount = act.discount / 10;
        } 
        if (act.id){ 
          that.setData({ 
            activity:act
          })
        }
      }
    )
  },
  setredtype:function(value){
    var redtype=this.data.redtype;
    var type=0;
    for (var i = 0; i < redtype.length;i++){
      if (redtype[i].value == value){
        redtype[i].checked=true;
        type = redtype[i].value
      }else{
        redtype[i].checked = false;
        type = redtype[i].value
      }
    }
    this.setData({
      redtype: redtype,
      type:type
    })
  },
  setredstate:function(value){
    var states = this.data.activitystate;
    for (var i = 0; i < states.length; i++) {
      if (states[i].value == value) {
        states[i].checked = true;
      } else {
        states[i].checked = false;
      }
    }
    this.setData({
      activitystate: states
    })
  },
  setdatetime:function(starttime,endtime){
    var st = starttime.split(" ");
    var et = starttime.split(" ");
    var time = { start: { date: st[0], time: st[1] }, end: { date: et[0], time: et[1] } }
    this.setData({
      time: time
    })
  },
  setsendtype:function(value){
    var sendtypes = this.data.sendtypes;
    var sendtypeindex=0;
    for (var i = 0; i < sendtypes.length; i++) {
      if (sendtypes[i].value == value) {
        sendtypeindex = value;
      } 
    }
    this.setData({ sendtypeindex: sendtypeindex})
  },
  bindback:function(){
    wx.navigateBack({})
  }
})